package dev.blogmoon.cn.common.enums;

import dev.blogmoon.cn.common.exception.BizException;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

public enum ErrorCode {

    SUCCESS(0, "成功"),
    PARAM_ERROR(5600, "参数错误"),
    SYSTEM_ERROR(5601, "系统错误"),
    LOGIN_ERROR(5602,"登录异常"),
    UNKNOW_CODE_ERROR(5603, "未知返回码错误"),
    REMOTE_ERROR(5604,"远程调用异常")
    ;
    @Getter
    private final int code;
    @Getter
    private final String msg;

    private static Map<Integer, ErrorCode> map;

    static {
        map = new HashMap<>();
        for (ErrorCode errorCodeEnum : ErrorCode.values()) {
            map.put(errorCodeEnum.getCode(), errorCodeEnum);
        }
    }

    ErrorCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static boolean isSuccess(int code) {
        return ErrorCode.SUCCESS.getCode() == code;
    }

    public static ErrorCode valueOf(Integer code) {
        return map.get(code);
    }


    public BizException throwBizException(){
        return new BizException(this.getCode(), this.getMsg());
    }
}
